package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategory2;
import com.atguigu.gmall.product.service.BaseCategory2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 一级分类controller层
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年06月25日 20:06
 * @ClassName:BaseCategory2Controller
 * @Description: TODO
 */
@RestController
@RequestMapping(value = "/category2")
public class BaseCategory2Controller {
    @Autowired
    private BaseCategory2Service baseCategory2Service;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping(value = "/findById/{id}")
    public Result<BaseCategory2> findById(@PathVariable(value = "id") Integer id){
        BaseCategory2 category2 = baseCategory2Service.getById(id);
        return Result.ok(category2);
    }
    /**
     * 查询所有的一级分类
     * @return
     */
    @GetMapping(value = "/findAll")
    public Result<List<BaseCategory2>> findAll(){
        List<BaseCategory2> baseCategory2s = baseCategory2Service.list(null);
        return Result.ok(baseCategory2s);
    }

    /**
     * 添加一级分类
     * @param baseCategory2
     * @return
     */
   @PostMapping(value = "/add")
    public Result save(@RequestBody BaseCategory2 baseCategory2){
        baseCategory2Service.save(baseCategory2);
        return Result.ok();
   }

    /**
     * 修改一级分类
     * @param baseCategory2
     * @return
     */
    @PutMapping(value = "/update")
    public Result update(@RequestBody BaseCategory2 baseCategory2){
        baseCategory2Service.updateById(baseCategory2);
        return Result.ok();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable(value = "id")Long id){
        baseCategory2Service.removeById(id);
        return Result.ok();
    }

    /**
     * 条件查询
     * @param baseCategory2
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<BaseCategory2>> search(@RequestBody BaseCategory2 baseCategory2){
        List<BaseCategory2> search = baseCategory2Service.search(baseCategory2);
        return Result.ok(search);
    }

    @GetMapping(value = "/getPage/{page}/{size}")
    public Result<List<BaseCategory2>> getPage(
                                               @PathVariable(value = "page")Integer page,
                                               @PathVariable(value = "size")Integer size){

        List<BaseCategory2> search = baseCategory2Service.pageSearch(page, size);
        return Result.ok(search);
    }

    /**
     * 分页条件查询
     * @param baseCategory2
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/searchByxx/{page}/{size}")
    public Result<List<BaseCategory2>> searchByxx(@RequestBody BaseCategory2 baseCategory2,
                                              @PathVariable(value = "page")Integer page,
                                              @PathVariable(value = "size")Integer size){

        List<BaseCategory2> search = baseCategory2Service.search(baseCategory2, page, size);
        return Result.ok(search);
    }
}
